package com.teragadgets.android.comics.appengine;

import java.io.IOException;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.labs.taskqueue.Queue;
import com.google.appengine.api.labs.taskqueue.QueueFactory;
import com.google.appengine.api.labs.taskqueue.TaskOptions;

public class CronJobServlet extends HttpServlet {

	@SuppressWarnings("unchecked")
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		Queue queue = QueueFactory.getDefaultQueue();
		PersistenceManager pm = PMF.get().getPersistenceManager();

		String query = "select id from " + WebComic.class.getName();
		List<Long> webComics = (List<Long>) pm.newQuery(query).execute();

		for (Long id : webComics) {
			queue.add(TaskOptions.Builder.url("/update?id=" + id).method(
					TaskOptions.Method.GET));
		}
	}
}
